import java.util.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;

public class Player
{
	private Card[] hand = new Card[5];
	public int bank;
	public int betAmount;
	public int numWins = 0, numLoss = 0;
	public String playerName;
	
	// Class constructor
	public Player()
	{
		bank = 1000;
		playerName = "Player";
		betAmount = 0;
		numWins = 0;
		numLoss = 0;
	}
	
	// Sets hand. (drawing 5 cards from deck)
	public void setHand(Deck deck)
	{
		for (int i=0; i<5; i++)
		{
			hand[i] = deck.getNewCard();
		}
	}
	
	// Get method for hand.
	public Card[] getHand()
	{
		return hand;
	}
	
	// Get method for a card in the players hand.
	public Card getCard(int index)
	{
		return hand[index];
	}
	
	// To see if the player has a winning hand.
	public int checkWin()
	{
		int[] suitArray = new int[4];
		int[] valueArray =  new int[13];
		int pairCount = 0, threeCount = 0, fourCount = 0, suitCount = 0;
		int seqCount = 0;
		int ord;
		boolean jacksBetter = false;
		
		try{
		for(int i=0; i<4; i++)
			suitArray[i] = 0;
			
		for(int i=0; i < 13; i++)
			valueArray[i] = 0;
			
		for(int i = 0; i < 5; i++)
		{
			ord = hand[i].suit.ordinal();
			suitArray[ord] ++;
		}
			
		for(int i=0; i<5; i++)
		{
			ord = hand[i].value.ordinal();
			valueArray[ord] ++;
		}
		
		for(int i=0; i < 4; i++)
			if(suitArray[i] == 5)
				suitCount = 1;
				
		for(int i=0; i<13; i++)
		{
			if(valueArray[i] == 2)
				pairCount ++;
			else if(valueArray[i] == 3)
				threeCount = 1;
			else if(valueArray[i] == 4)
				fourCount = 1;
			if(i!=12)	
				if(valueArray[i] != 0 && valueArray[i] == valueArray[i+1] && i != 12)
					seqCount ++;
		}
		for(int i = 9; i < 13; i++)
			if(valueArray[i] == 2) jacksBetter = true;
			
		} catch(ArrayIndexOutOfBoundsException e)
			{	System.out.print("error");}
		if(seqCount == 4 && suitCount == 4)
			return 1;
		if(seqCount == 4 && suitCount == 1)
			return 2;
		else if(seqCount == 4)
			return 3;
		else if(suitCount == 1)
			return 4;
		else if(pairCount == 1 && threeCount == 1)
			return 5;
		else if(fourCount == 1)
			return 6;
		else if(threeCount == 1)
			return 7;
		else if(pairCount == 2)
			return 8;
		else if(jacksBetter)
			return 9;
		else
			return 0;
		
	}
	
	// Loads a saved game from save.txt when passed the name of the player.
	public void loadGame(String string) {
		try {
			Scanner input = new Scanner(new File("save.txt"));
			String s = input.next();
			while(input.hasNext() && !(s.equals(string))) {
				s = input.next();
			}
			playerName = s;
			bank = input.nextInt();
			numWins = input.nextInt();
			numLoss = input.nextInt();
			Game.nameLabel.setText("Name: " + playerName);
			Game.bankLabel.setText("Bank: $" + Integer.toString(bank));
			
			//Maybe this could go in Game somewhere.  Disables bets higher than the players bank.
			if(bank < 10) Game.radioButton[0].setEnabled(false);
			else Game.radioButton[0].setEnabled(true);
			if(bank < 25) Game.radioButton[1].setEnabled(false);
			else Game.radioButton[1].setEnabled(true);
			if(bank < 50) Game.radioButton[2].setEnabled(false);
			else Game.radioButton[2].setEnabled(true);
			if(bank < 100) Game.radioButton[3].setEnabled(false);
			else Game.radioButton[3].setEnabled(true);
			if(bank < 150) Game.radioButton[4].setEnabled(false);
			else Game.radioButton[4].setEnabled(true);
			if(bank < 200) Game.radioButton[5].setEnabled(false);
			else Game.radioButton[5].setEnabled(true);
			if(bank < 250) Game.radioButton[6].setEnabled(false);
			else Game.radioButton[6].setEnabled(true);
			if(bank < 500) Game.radioButton[7].setEnabled(false);
			else Game.radioButton[7].setEnabled(true);
			
			input.close();
		}
		catch(java.io.FileNotFoundException e) {
			JOptionPane.showMessageDialog(null, "The save file save.txt was not found...", "Error", JOptionPane.WARNING_MESSAGE);
		}
		catch(java.util.NoSuchElementException e) {}
	}
	
	// Saves the current game to save.txt (overwrites if player name already exists, appends for new players.
	public void saveGame(String string) {
		try {
			// Open save.txt for reading
			Scanner findName = new Scanner(new File("save.txt"));
			
			// Make an array of players
			Player[] playerList = new Player[100];
			for(int i = 0; i < 100; i ++)
				playerList[i] = new Player();
			
			// Copy each player in save.txt to the array
			for(int i = 0; findName.hasNext(); i++) {
				playerList[i].playerName = findName.next();
				playerList[i].bank = findName.nextInt();
				playerList[i].numWins = findName.nextInt();
				playerList[i].numLoss = findName.nextInt();
			}
			
			findName.close();
			
			// Open save.txt for writing
			PrintWriter output = new PrintWriter(new File("save.txt"));
			for(int i = 0; i < playerList.length; i++) {
				if(playerList[i].playerName.equals(string)) {
					output.println(playerName);
					output.println(bank);
					output.println(numWins);
					output.println(numLoss);
				}
				else {
					if(!playerList[i].playerName.equals("Player")) {
						output.println(playerList[i].playerName);
						output.println(playerList[i].bank);
						output.println(playerList[i].numWins);
						output.println(playerList[i].numLoss);
					}
				}
			}
			output.close();
		}
		catch(java.io.FileNotFoundException e) {
			JOptionPane.showMessageDialog(null, "The save file save.txt was not found...", "Error", JOptionPane.WARNING_MESSAGE);
		}
		catch(java.util.NoSuchElementException e) {}
	}
}
	

